package cn.how2j.basics.advanced;

import cn.hutool.json.JSONUtil;
import org.w3c.dom.ls.LSOutput;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by SilentJhin on 2020/7/31 13:20
 * y 代表年
 * M 代表月
 * d 代表日
 * H 代表24进制的小时
 * h 代表12进制的小时
 * m 代表分钟
 * s 代表秒
 * S 代表毫秒
 */
public class DateTest {
    public static void main(String[] args) {
        // 使用单例模式获取日历对象
        Calendar c = Calendar.getInstance();
        // 通过日历对象得到日期对象
        Date d = c.getTime();
        //Date d2 = new Date(0);
        //c.setTime(d2); // 1970.1.1 08:00:00
        //System.out.println(d.toString());
        //System.out.println(d2.toString());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //System.out.println("当前 " + sdf.format(d));
        c.setTime(d);
        c.add(Calendar.YEAR, 1);
        //System.out.println("下年 " + sdf.format(c.getTime()));
        c.setTime(d);
        c.add(Calendar.YEAR, -1);
        //System.out.println("去年 " + sdf.format(c.getTime()));
        c.setTime(d);
        c.add(Calendar.MONTH, 1);
        //System.out.println("下月 " + sdf.format(c.getTime()));
        c.setTime(d);
        c.add(Calendar.MONTH, -1);
        //System.out.println("上月 " + sdf.format(c.getTime()));
        c.setTime(d);
        c.add(Calendar.DATE, 1);
        //System.out.println("明天 " + sdf.format(c.getTime()));
        c.setTime(d);
        c.add(Calendar.DATE, -1);
        //System.out.println("昨天" + sdf.format(c.getTime()));

        //c.setTime(d);
        c.add(Calendar.HOUR, 1);
        //System.out.println("下一小时 " + sdf.format(c.getTime()));
        c.setTime(d);
        c.add(Calendar.HOUR, -1);
        //System.out.println("上一小时" + sdf.format(c.getTime()));
        c.setTime(d);
        c.add(Calendar.MINUTE, 1);
        //System.out.println("下一分钟 " + sdf.format(c.getTime()));
        c.setTime(d);
        c.add(Calendar.MINUTE, -1);
        //System.out.println("上一分钟" + sdf.format(c.getTime()));
        c.setTime(d);
        c.add(Calendar.SECOND, 1);
        //System.out.println("下一秒 " + sdf.format(c.getTime()));
        c.setTime(d);
        c.add(Calendar.SECOND, -1);
        //System.out.println("上一秒" + sdf.format(c.getTime()));

        System.out.println(sdf.format(getMonthBegins()));
        System.out.println(sdf.format(getMonthEnd()));

        //Date d1 = new Date();
        //Date d2 = new Date(5000);
        //System.out.println("直接打印 " + d1);
        //System.out.println("toString " + d1.toString());
        //System.out.println("getTime " + d1.getTime());
        //System.out.println("System.currentTimeMillis():" + System.currentTimeMillis());
        //System.out.println("5000 " + d2);

        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSSS");
        //SimpleDateFormat sdf2 = new SimpleDateFormat("yy-MM-dd");
        //System.out.println("格式化 " + sdf.format(d1));
        //System.out.println("格式化2 " + sdf2.format(d1));
        //String parseStr = "20-07-15";
        //try {
            // java.text.ParseException
            //System.out.println(sdf.parse(parseStr));
        //} catch (Exception e) {
        //    e.printStackTrace();
        //}
    }

    // 获取月初
    public static Date getMonthBegins(){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.DATE, 1);
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c.getTime();
    }

    // 获取月末
    public static Date getMonthEnd(){
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.DATE, 1);
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.add(Calendar.MONTH, 1);
        c.add(Calendar.DATE, -1);
        return c.getTime();
    }

    // 获取本年的最后一个月


}